<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="医生名称" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入医生名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="护士名称" prop="nurseName">
        <el-input
          v-model="queryParams.nurseName"
          placeholder="请输入护士名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班组名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入班组名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出车状态" prop="carStatus">
        <el-select
          v-model="queryParams.carStatus"
          placeholder="请选择出车状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in carStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:class:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:class:edit']"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:class:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['manager:class:export']"-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table
      v-loading="loading"
      :data="classList"
      @selection-change="handleSelectionChange"
      height="67vh"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="classID" />
      <el-table-column label="班组名称" align="center" prop="name" />
      <el-table-column label="医生名称" align="center" prop="doctorName" />
      <el-table-column label="护士名称" align="center" prop="nurseName" />
      <!-- <el-table-column label="班组标识" align="center" prop="classID" /> -->
      <!-- <el-table-column label="医生标识" align="center" prop="doctorID" /> -->
      <!-- <el-table-column label="护士标识" align="center" prop="nurseID" /> -->
      <el-table-column
        label="排班信息"
        align="center"
        prop="weekID"
        :formatter="weekIDFormat"
      />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="出车状态"
        align="center"
        prop="carStatus"
        :formatter="carStatusFormat"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['manager:class:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            v-hasPermi="['manager:class:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班组管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班组名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入班组名称" />
        </el-form-item>
        <el-form-item label="医生" prop="doctorID">
          <!-- <el-input v-model="form.doctorID" placeholder="请输入医生标识" /> -->
          <el-select v-model="form.doctorID" placeholder="请选择医生">
            <el-option
              v-for="depart in doctorList"
              :key="depart.doctorID"
              :label="depart.name"
              :value="depart.doctorID"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="护士" prop="nurseID">
          <!-- <el-input v-model="form.nurseID" placeholder="请输入护士标识" /> -->
          <el-select v-model="form.nurseID" placeholder="请选择护士">
            <el-option
              v-for="depart in nurseList"
              :key="depart.nurseID"
              :label="depart.name"
              :value="depart.nurseID"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="出车状态">
          <el-select v-model="form.carStatus" placeholder="请选择出车状态">
            <el-option
              v-for="dict in carStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排班" prop="weekList">
          <el-checkbox-group
            v-model="form.weekList"
            size="medium"
            @change="handleWeekClick"
          >
            <el-checkbox
              v-for="(item, index) in weekIDOptions"
              :key="index"
              :label="item.dictValue"
              :disabled="item.disabled"
              name="weekType"
              >{{ item.dictLabel }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClass,
  getClass,
  delClass,
  addClass,
  updateClass,
  exportClass,
  listDoctor
} from "@/api/biz/class";
import { listNurse } from "@/api/biz/nurse";
export default {
  name: "Class",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 班组管理表格数据
      classList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 排班信息字典
      weekIDOptions: [],
      // 医生列表
      doctorList: [],
      // 护士列表
      nurseList: [],
      // 出车状态字典
      carStatusOptions: [],
      // 状态字典
      statusOptions: [],
      // 医生状态查询
      doctorQueryParams: {
        classStatus: 0,
        doctorID: null,
        isMaster: null,
        accountStatus: 0,
        status: 0
      },
      // 护士状态查询
      nurseQueryParams: {
        classStatus: 0,
        nurseID: null,
        accountStatus: 0,
        status: 0
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorName: undefined,
        nurseName: undefined,
        name: undefined,
        weekID: undefined,
        carStatus: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        weekList: []
      },
      // 表单校验
      rules: {
        uid: [{ required: true, message: "医院标识不能为空", trigger: "blur" }],
        name: [
          { required: true, message: "班组名称不能为空", trigger: "blur" }
        ],
        doctorID: [
          { required: true, message: "医生标识不能为空", trigger: "blur" }
        ],
        nurseID: [
          { required: true, message: "护士标识不能为空", trigger: "blur" }
        ],
        // carStatus: [
        //   { required: true, message: '出车状态不能为空', trigger: 'blur' }
        // ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        weekList: [
          {
            required: true,
            type: "array",
            message: "请至少选择一个多选框组",
            trigger: "change"
          }
        ]
      }
    };
  },
  created() {
    this.getList();

    this.getDicts("class_info").then(response => {
      this.weekIDOptions = response.data;
    });
    this.getDicts("class_car_status").then(response => {
      this.carStatusOptions = response.data;
    });
    this.getDicts("common_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询班组管理列表 */
    getList() {
      this.loading = true;
      listClass(this.queryParams).then(response => {
        this.classList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取医生列表
    getDoctorList() {
      listDoctor(this.doctorQueryParams).then(response => {
        this.doctorList = response.rows;
      });
    },
    // 获取护士列表
    getNurseList() {
      listNurse(this.nurseQueryParams).then(response => {
        this.nurseList = response.rows;
      });
    },
    // 排班信息字典翻译
    weekIDFormat(row, column) {
      if (row.weekID != null) {
        var weekID = this.handlesplit(row.weekID);
        let weekIDOptions = this.weekIDOptions;
        let weekIDOptionsArr = [];
        let weekIDs = [];
        weekIDOptions.forEach(ele => {
          weekIDOptionsArr[ele.dictValue] = ele.dictLabel;
        });
        weekID.forEach(ele => {
          weekIDs.push(weekIDOptionsArr[ele]);
        });
        // return this.handlejoin(weekIDs)
        // 更改数据分隔符为 /
        return weekIDs.join("/");
      } else {
        return row.weekID;
      }

      // return this.selectDictLabel(this.weekIDOptions, row.weekID)
    },

    // 出车状态字典翻译
    carStatusFormat(row, column) {
      return this.selectDictLabel(this.carStatusOptions, row.carStatus);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        doctorName: undefined,
        nurseName: undefined,
        classID: undefined,
        uid: undefined,
        name: undefined,
        doctorID: undefined,
        nurseID: undefined,
        weekID: undefined,
        remark: undefined,
        carStatus: undefined,
        status: undefined,
        weekList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.classID);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.doctorQueryParams.doctorID = null;
      this.nurseQueryParams.nurseID = null;
      this.getDoctorList();
      this.getNurseList();
      this.open = true;
      this.title = "添加班组管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.doctorQueryParams.doctorID = row.doctorID;
      this.nurseQueryParams.nurseID = row.nurseID;
      this.getDoctorList();
      this.getNurseList();
      const classID = row.classID || this.ids;
      getClass(classID).then(response => {
        response.data.weekList = this.handlesplit(response.data.weekID);
        this.form = response.data;
        this.open = true;
        this.title = "修改班组管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.weekID = this.handlejoin(this.form.weekList);
          if (this.form.classID !== undefined) {
            updateClass(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addClass(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const classIDs = row.classID || this.ids;
      this.$confirm(
        '是否确认删除班组管理编号为"' + classIDs + '"的数据项?',
        "警告:(删除后医生护士资源将解放)",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delClass(classIDs);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功且资源已释放");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有班组管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportClass(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    },
    /** 字符串转换数组 */
    handlesplit(data) {
      return data.split(",");
    },
    /** 数组转换字符串 */
    handlejoin(data) {
      return data.join(",");
    },
    handleWeekClick() {
      this.form.weekList = this.form.weekList.sort();
    }
  }
};
</script>
<style scoped="scoped">
/* 分页 */
.pagination-container {
  height: 80px;
  margin-top: 0px;
  padding: 23px 20px !important;
}
/* 分页  end */
</style>
